﻿package problems.readerswriters;

import problems.BenchmarkSemaphore;

class LightSwitch
{
    private int counter;
    private final BenchmarkSemaphore mutex;
    
    public LightSwitch()
    {
        mutex = new BenchmarkSemaphore(1);
    }

    public void lock(BenchmarkSemaphore BenchmarkSemaphore)
    {
        mutex.waitOne();
        counter += 1;
        if (counter == 1) BenchmarkSemaphore.waitOne();
        mutex.release();
    }

    public void unlock(BenchmarkSemaphore BenchmarkSemaphore)
    {
        mutex.waitOne();
        counter -= 1;
        if (counter == 0) BenchmarkSemaphore.release();
        mutex.release();
    }
}